# from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractUser
from areas.models import Area
# Create your models here.
from meiduo_mall.utils.models import BaseModel
#修改的时间和创建的时间



class User(AbstractUser):
    # """自定义用户模型类"""添加字段，用户邮箱是否被激活
    mobile = models.CharField(max_length=11)
    email_active = models.BooleanField(default=False)
    #设置默认地址的字段，
    default_address = models.ForeignKey('Address', related_name='users',null=True)
    class Meta:
        db_table = 'tb_users'


# 建立收货地址的模型类
class Address(BaseModel):
#建立一个表，储存数据库的的收货地址
    #确立是那个用户的登陆
    user = models.ForeignKey(User,related_name='addresses')
    #标题
    title = models.CharField(max_length= 10)
    #接受人
    receiver = models.CharField(max_length=10)
    #省份
    province = models.ForeignKey(Area,related_name='provinces')
    #城市
    city = models.ForeignKey(Area,related_name='cities')
    #区县
    district = models.ForeignKey(Area,related_name='districts')
    #详细地址
    detail = models.CharField(max_length= 50)
    #联系电话
    mobile = models.CharField(max_length=30)
    # 固定电话
    phone =  models.CharField(max_length=30,null=True)
    #邮件
    email = models.CharField(max_length= 30,null= True)
    #逻辑删除
    is_delete = models.BooleanField(default= False)


    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'receiver': self.receiver,
            'province_id': self.province_id,
            'province': self.province.name,
            'city_id': self.city_id,
            'city': self.city.name,
            'district_id': self.district_id,
            'district': self.district.name,
            'place': self.detail,
            'mobile': self.mobile,
            'tel': self.phone,
            'email': self.email
        }

